package in.muton.edushare.poll.questions;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.Collections;
import java.util.List;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.vo.PollOptions;
import com.edushare.common.vo.PollQuestions;

public class DropdownQuestion extends QuestionBase {
	public DropdownQuestion(String id, IModel<PollQuestions> model,
			EduShareBasePage page, final boolean preview, final boolean editMode) {
		super(id, model, page, preview, editMode);
		List<PollOptions> poList = getQuestionModel().getObject().getOptions();
		Collections.sort(poList);
		final CayenneList<PollOptions> po = new CayenneList<PollOptions>(poList);
		retrieveValues();
		DropDownChoice<IModel<PollOptions>> ddc;
		add(ddc = new DropDownChoice<IModel<PollOptions>>("connective", po
				.getObject(), new IChoiceRenderer<IModel<PollOptions>>() {
			private static final long serialVersionUID = 3064147159987024023L;

			public Object getDisplayValue(IModel<PollOptions> arg0) {
				return arg0.getObject().getOptionString();
			}

			public String getIdValue(IModel<PollOptions> arg0, int arg1) {
				return arg0.getObject().getObjectId().toString();
			}
		}));
		ddc.setModel(new Model<IModel<PollOptions>>() {
			private static final long serialVersionUID = 6780267902923551599L;

			@Override
			public void setObject(IModel<PollOptions> p) {
				setSelection(p.getObject().getOptionId());
			}

			public IModel<PollOptions> getObject() {
				List<IModel<PollOptions>> poList = po.getObject();
				for (IModel<PollOptions> p : poList) {
					if (p.getObject().getOptionId() == selection)
						return p;
				}
				return null;
			}
		});
	}

	private Integer selection = null;
	private static final long serialVersionUID = 7079929247501262875L;

	public void setSelection(Integer selection) {
		this.selection = selection;
	}

	public Integer getSelection() {
		return selection;
	}

	@Override
	public boolean retrieveValues() {
		UserPollDAO upDAO = new UserPollDAO(getORMInterface());
		List<PollOptions> poList = upDAO.getAnswer(getLoggedInUser(),
				getQuestionModel().getObject());
		if (poList.size() != 1)
			return false;
		setSelection(poList.get(0).getOptionId());
		return true;
	}

	@Override
	public boolean save(boolean draft) {
		if (isPreview() || isEditMode() || (!draft && !verify()))
			return false;
		UserPollDAO uPDao = new UserPollDAO(getORMInterface());
		uPDao.setAnswer(getLoggedInUser(), getQuestionModel().getObject(),
				getSelection());
		return true;
	}

	@Override
	public boolean verify() {
		if (getSelection() == null && getQuestionModel().getObject().isRequired()) {
			setError("Please make a selection for question no. "
					+ getQuestionModel().getObject().getQuestionSerialNo());
			return false;
		}
		return true;
	}

	@Override
	public void clear() {
		new UserPollDAO(getORMInterface()).clearAnswers(getLoggedInUser(),
				getQuestionModel().getObject());
		setSelection(null);
	}
}
